// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include "GameManager.h"
#include "Spaceship.h"
#include "Planet.h"
#include "UnitDecorator.h"

using namespace std;

UnitDecorator::UnitDecorator(void)
{

}

UnitDecorator::~UnitDecorator(void)
{

}

//loop thr all the space obj and display associated icon
void UnitDecorator::decorateUnit(SpaceObject* unit, VIEW* camera)
{
	// don't do this for omega7
	Planet* p = dynamic_cast<Planet*>(unit);
	if (!(p != 0 && p->maxPopulation() < 0)) {
		VECTOR shipPos; //position of spaceship

		//retrieve ship pos in game world
		unit->getPosition().toGamestudio(&shipPos);

		//transform ship coord to screen coord, z-axis = distance
		if(vec_to_screen(&shipPos, camera)!=0){//if visible on screen
			unit->getDecorationPanel()->setPosition(
				make_pair(_INT(shipPos.x) - unit->getDecorationPanel()->getSize().first / 2,
						  _INT(shipPos.y) - unit->getDecorationPanel()->getSize().second / 2));
			unit->getDecorationPanel()->display();
		} else {
			unit->getDecorationPanel()->hide();
		}
	} else {
		unit->getDecorationPanel()->hide();
	}
}

string UnitDecorator::getIcon(SpaceObject *so)
{
	GameManager* gm = GameManager::getInstance();
	Player* pl = gm->getPlayers()[0]; //get human player

	// First part: depends on type of object
	string res;
	if (typeid(*so) == typeid(Planet)) {
		res = "planet";
	} else if (typeid(*so) == typeid(Spaceship)) {
		Spaceship* ss = dynamic_cast<Spaceship*>(so);
		res = Spaceship::stringFromSpaceshipType(ss->getType());
	} else {
		return "";
	}

	if(so->getOwner()==pl) { //player's ship
		res += "_g.png";
	} else if (so->getOwner() != 0) { //enemy's ships
		res += "_r.png";
	} else {
		res += "_b.png"; // neutral
	}

	return res;
}